/**
 * <p>Title: BaseAction.java</p>
 * <p>Description: DoNotes</p>
 * @author WangJun
 * @date 2009/11/30 11:09:28
 * @version 1.0
 */
package net.donotes.blog.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.donotes.blog.dao.CategoryDao;
import net.donotes.blog.dao.TagDao;
import net.donotes.blog.pojo.Category;
import net.donotes.blog.pojo.Post;
import net.donotes.blog.pojo.Tag;
import net.donotes.blog.service.PostService;

import org.apache.struts2.ServletActionContext;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.opensymphony.xwork2.ActionSupport;

/**
 * @author WangJun
 *
 */
public class BaseAction extends ActionSupport {

	private static final long serialVersionUID = 1L;

	private List<Post> recentPost = null;
	private List<Category> categorys = null;
	private List<Tag> recentTag = null;
	private User user = null;
	private String loginURL = "";
	private String logoutURL = "";
	
	public BaseAction() {
		PostService postServiec = new PostService();
		TagDao tagDao = new TagDao();
		CategoryDao categoryDao = new CategoryDao();
        UserService userService = UserServiceFactory.getUserService();

        this.user = userService.getCurrentUser();
		this.recentPost = postServiec.listPost();
		this.recentTag = tagDao.listTag();
		this.categorys = categoryDao.listCategory();
	}	
	
	/**
	 * @return the loginURL
	 */
	public String getLoginURL() {
 		HttpServletRequest request = ServletActionContext.getRequest();
        UserService userService = UserServiceFactory.getUserService();

        Object requestUri = request.getAttribute("struts.request_uri");
        String backRUL = "/blog";
        if (requestUri != null) {
        	backRUL = requestUri.toString(); 
        }
		
		if (request.getQueryString() != null) {
			backRUL = backRUL + "?" + request.getQueryString();
		} else {
			if (request.getAttribute("postId") != null) {
				backRUL = backRUL + "?postId=" + request.getAttribute("postId");
			}
		}

        return userService.createLoginURL(backRUL);
	}

	/**
	 * @return the logoutURL
	 */
	public String getLogoutURL() {
		HttpServletRequest request = ServletActionContext.getRequest();
        UserService userService = UserServiceFactory.getUserService();

        Object requestUri = request.getAttribute("struts.request_uri");
        String backRUL = "/blog";
        if (requestUri != null) {
        	backRUL = requestUri.toString(); 
        }
		if (request.getQueryString() != null) {
			backRUL = backRUL + "?" + request.getQueryString();
		}

        return userService.createLogoutURL(backRUL);
	}

	/**
	 * @return the recentPost
	 */
	public List<Post> getRecentPost() {
		return recentPost;
	}

	/**
	 * @return the categorys
	 */
	public List<Category> getCategorys() {
		return categorys;
	}

	/**
	 * @return the recentTag
	 */
	public List<Tag> getRecentTag() {
		return recentTag;
	}

	/**
	 * @return the user
	 */
	public User getUser() {
		return user;
	}
}